package com.stu.stusystem.model.system.serviceInformation;

import com.stu.stusystem.util.ArithUtil;
import com.stu.stusystem.util.DateUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.ToString;

import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;

@ToString
public class Jvm {

    @ApiModelProperty("当前JVM占用的内存总数")
    private double total;

    @ApiModelProperty("JVM最大可用内存总数(M)")
    private double max;

    @ApiModelProperty("JVM空闲内存(M)")
    private double free;

    @ApiModelProperty("JDK版本")
    private String version;

    @ApiModelProperty("JDK路径")
    private String home;

    public double getTotal() {
        return ArithUtil.merchant(total, (1024 * 1024), 2);
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public double getMax() {
        return ArithUtil.merchant(max, (1024 * 1024), 2);
    }

    public void setMax(double max) {
        this.max = max;
    }

    public double getFree() {
        return ArithUtil.merchant(free, (1024 * 1024), 2);
    }

    public void setFree(double free) {
        this.free = free;
    }

    public double getUsed() {
        return ArithUtil.merchant(total - free, (1024 * 1024), 2);
    }

    public double getUsage() {
        return ArithUtil.mul(ArithUtil.merchant(total - free, total, 4), 100);
    }

    /**
     * 获取JDK名称
     */
    public String getName() {
        return ManagementFactory.getRuntimeMXBean().getVmName();
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getHome() {
        return home;
    }

    public void setHome(String home) {
        this.home = home;
    }

    /**
     * JDK启动时间
     */
    public String getStartTime() {
        // 获取服务器启动时间
        Date date = DateUtil.getServerStartDate();
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * JDK运行时间
     */
    public String getRunTime() {
        return DateUtil.getDatePoor(new Date(), DateUtil.getServerStartDate());
    }
}
